package com.tengju.bff.interfaces.miniprogram.user.facade;

import com.tengju.bff.interfaces.miniprogram.user.vo.UserInfoVo;
import com.tengju.bff.interfaces.shared.servlet.FromEnum;
import com.tengju.user.application.user.UserInfoService;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.service.RemoteUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @since: 2021/12/30 11:09 上午
 * @author: xiajichen
 */
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class UserInfoFacade {

    private final UserInfoService userInfoService;
    private final RemoteUserService remoteUserService;

    public UserInfoVo getUserInfoByFrom(UserIdCode userIdCode, String from) {
        UserInfo userInfoByFrom = null;
        // 根据请求来自哪个业务域，返回对应业务域的用户信息
        if (StringUtils.isEmpty(from) || FromEnum.DT.getValue().equals(from)) {
            userInfoByFrom = userInfoService.getUserInfo(userIdCode);
        } else if (FromEnum.ABM.getValue().equals(from)) {
            userInfoByFrom = remoteUserService.getAbmUserByIdCode(userIdCode.getValue());
        }
        if (null == userInfoByFrom) {
            return new UserInfoVo(
                    null,
                    null,
                    null,
                    null,
                    null
            );
        }
        return new UserInfoVo(
                userInfoByFrom.getUserId().getValue(),
                userInfoByFrom.getIdCode().getValue(),
                userInfoByFrom.getNickName(),
                userInfoByFrom.getHeadImage(),
                null == userInfoByFrom.getStarLevel() ?  userInfoByFrom.getAbmLevel() : userInfoByFrom.getStarLevel()
        );
    }

}
